import {promiseRequest } from "../../../utils/index";
const app = getApp();
Page({
  data: {
    receiveAddress:'',
    tele:'',
    zipcode:'',
    modalshow:'',
    id:''
  },
//页面加载的时候根据openid查询获取用户的手机号和昵称信息
onLoad(query) {
  //根据openid查询当前人的手机号
  let openid = query.lqropenid;
  let actId = query.actId;
  this.queryAddress(openid);
},
onShow(){
},
bindreceiveAddress(e){
  this.setData({
    receiveAddress: e.detail.value
  })  
},
bindtele(e) {
  this.setData({
    tele: e.detail.value
  })
},
bindzipcode(e) {
  this.setData({
    zipcode: e.detail.value
  })
},
//查询收货地址信息
queryAddress(openid) {
  let requestObj = {
    url: '/wxa/actaddress/detail',
    method: 'post',
    dataobj: {openid:openid},
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      let receiveAddress = '';
      let tele = '';
      let zipcode = '';
      let id = '';
      if (res.data) {
        receiveAddress = res.data.receiveAddress;
        tele = res.data.tele;
        zipcode = res.data.tele;
        id = res.data.id;
        this.setData({
          receiveAddress: receiveAddress,
          tele: tele,
          zipcode: tele
        })
        this.data.id = id;
      }
    }
  }).catch((errMsg) => {
  });
},
//获取用户手机号触发的事件
getPhoneNumber(e) {
  //同意获取手机信息
  if (e.detail.errMsg == 'getPhoneNumber:ok') {
    wx.checkSession({
      success: () => {
        this.decodeUserPhone(e.detail.iv, e.detail.encryptedData);
      },
      fail: () => {
        // 重新授权
        // getToken().then(() => {
        //   this.decodeUserPhone(e.detail.iv, e.detail.encryptedData);
        // })
      }
    })
  } else {

  }
},
decodeUserPhone(iv, encryptedData) {
  let requestObj = {
    url: '/wxa/user/decodeUser',
    method: 'post',
    dataobj: { iv: iv, encryptedData: encryptedData},
  };
  promiseRequest(requestObj).then((res) => {
    if (res.data) {
      this.setData({
        tele: res.data.phoneNumber
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '自动获取手机号码失败，请手动输入',
        success: (res) => {
          wx.navigateTo({
            url: '/pages/compent/telCheck/telCheck'
          })
        }
      })
    }
  }).catch((errMsg) => {
  });
},
//保存收货地址
saveaddress() {
  let receiveAddress = this.data.receiveAddress;//收货地址
  let tele = this.data.tele;//收货电话
  let zipcode = this.data.zipcode;//邮政编码
  if (receiveAddress==''){
    wx.showToast({
      title: '收货地址不能为空!',
      icon: 'none'
    });   
    return; 
  }
  if (tele == '') {
    wx.showToast({
      title: '电话不能为空!',
      icon: 'none'
    });
    return; 
  }
  //保存商家信息
  var address = {id:this.data.id,receiveAddress: receiveAddress, tele: tele, zipcode:zipcode};
  let requestObj = {
    url: '/wxa/actaddress/save',
    method: 'post',
    dataobj: address,
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      this.setData({
        modalshow: 'show'
      })
    }
  }).catch((errMsg) => {
  });
},
hideModal(e) {
  this.setData({
    modalshow: ''
  })
},
//查看更多优惠活动
moreshare() {
  wx.switchTab({
    url: '../../tabar/index/index',
  })
},
})